package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.CoverDto;
import com.hualude.roadsign.repository.CoverFaultRepository;
import com.hualude.roadsign.repository.CoverRepository;
import com.hualude.roadsign.service.CoverManageService;
import com.hualude.roadsign.utils.FormatNumber;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CoverManageServiceImpl implements CoverManageService {

    @Resource
    private CoverRepository coverRepository;
    @Resource
    private CoverFaultRepository coverFaultRepository;

    public DTResponseDto getCover4DT(DTRequestDto dtRequestDto,
                                     String actorIds,
                                     Integer orgId,
                                     Boolean audit,
                                     String startRoad,
                                     String endRoad,
                                     String roadName) throws Exception {
        List<CoverDto> CoverDtos = coverRepository.getCover4DT(
                dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, roadName);
        Integer count = coverRepository.getCover4DTCount(
                dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, roadName);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.draw, count, count, CoverDtos);
        return dtResponseDto;
    }


    @Override
    @Transactional
    public Integer deleteCover(String coverIds) throws Exception {
        String[] coverIdArray = coverIds.split(",");
        for (String coverId : coverIdArray) {
            coverFaultRepository.deleteCoverFault(Integer.parseInt(coverId));
            coverRepository.deleteCover(Integer.parseInt(coverId));
        }
        return coverIdArray.length;
    }

    @Override
    public Integer addCover(CoverDto coverDto) throws Exception {
        coverDto.setCoverLongitude(FormatNumber.resize(coverDto.getCoverLongitude()));
        coverDto.setCoverLatitude(FormatNumber.resize(coverDto.getCoverLatitude()));
        return coverRepository.save(coverDto.toBean()).getCoverId();
    }

    @Override
    @Transactional
    public Integer updateCover(CoverDto coverDto) throws Exception {
        return coverRepository.updateCover(
                coverDto.getCoverId(),
                coverDto.getProjectNumber(),
                coverDto.getCoverLongitude(),
                coverDto.getCoverLatitude(),
                coverDto.getCoverProvince(),
                coverDto.getCoverCity(),
                coverDto.getCoverArea(),
                coverDto.getCoverRoadName(),
                coverDto.getCoverStartRoad(),
                coverDto.getCoverEndRoad(),
                coverDto.getCoverShape(),
                coverDto.getCoverMaterial(),
                coverDto.getCoverSize(),
                coverDto.getCoverPhoto(),
                coverDto.getCoverInstallTime(),
                coverDto.getCoverInstallOrg(),
                coverDto.getCoverPeriod(),
                coverDto.getCoverServiceLife(),
                coverDto.getCoverRemark(),
                coverDto.getCoverCreatorId(),
                coverDto.getCoverModifierId(),
                coverDto.getCoverOrgId(),
                coverDto.getCoverMainOrgId());
    }

    @Override
    public List<String> getStartRoadByRoadName(String roadName, String actorIds, Integer orgId) throws Exception {
        return coverRepository.getStartRoadByRoadName(roadName, actorIds, orgId);
    }

    @Override
    public List<String> getEndRoadByStartRoad(String city, String coverRoadName, String startRoad, String actorIds, Integer orgId) throws Exception {
        return coverRepository.getEndRoadByStartRoad(city, coverRoadName, startRoad, actorIds, orgId);
    }

    @Override
    public List<CoverDto> getCover4APP(String actorIds,
                                       Integer orgId,
                                       Integer mCount,
                                       Integer mSize,
                                       String city,
                                       String startRoad,
                                       String endRoad,
                                       String roadName) throws Exception {
        return coverRepository.getCover4APP(actorIds, orgId, mCount, mSize, city, startRoad, endRoad, roadName);
    }

    @Override
    public CoverDto getCoverById(Integer coverId) throws Exception {
        return coverRepository.getCoverById(coverId).get(0);
    }

    @Override
    public List<String> getCity4Cover4App(String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return coverRepository.getCity4Cover4App(actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<String> getRoadName4CoverByCity(String actorIds, Integer orgId, String city, Integer mCount, Integer mSize) throws Exception {
        return coverRepository.getRoadName4CoverByCity(actorIds, orgId, city, mCount, mSize);
    }

    @Override
    public List<String> getEndRoadByStartRoad4App(String city, String coverRoadName, String startRoad, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return coverRepository.getEndRoadByStartRoad4App(city, coverRoadName, startRoad, actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<String> getStartRoadByRoadName4App(String city, String roadName, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return coverRepository.getStartRoadByRoadName4App(city, roadName, actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<CoverDto> getCover4Map4App(Integer orgId,
                                           String actorIds,
                                           double neLongitude,
                                           double neLatitude,
                                           double swLongitude,
                                           double swLatitude) throws Exception {
        return coverRepository.getCover4Map4App(orgId, actorIds, neLongitude, neLatitude, swLongitude, swLatitude);
    }


    @Override
    @Transactional
    public Integer auditCover(String coverIds) throws Exception {
        String[] coverIdArray = coverIds.split(",");
        for (String coverId : coverIdArray) {
            coverRepository.auditCover(Integer.parseInt(coverId));
        }
        return coverIdArray.length;
    }
}
